package com.lovepick;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.lovepick.database.MsgListTable;
import com.lovepick.fragment.MsgFragment;
import com.lovepick.util.Constant;


public class MsgActivity extends FragmentActivity
{
	private static final String TAG = "MsgActivity";
	private static final String FTAG_MSG = "msg";
	private Fragment mFragment = null;
	private MsgReceiver mReceiver = null;
	private int mUserID;

	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.msg_activity);

		Bundle data = getIntent().getBundleExtra("bundle_msg");
		if (data != null)
		{
			Log.i(TAG, "## list_bundle!=null ##");
			mUserID = data.getInt(MsgFragment.ARG_USERID);
			mFragment = MsgFragment.createInstance(data);
		}

		getActionBar().setDisplayHomeAsUpEnabled(true);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.add(R.id.container, mFragment, FTAG_MSG).commit();

		mReceiver = new MsgReceiver();
		IntentFilter msgFilter = new IntentFilter(Constant.ACTION_NEW_MSGS);
		registerReceiver(mReceiver, msgFilter);

		// 发送界面开启广播, 新消息获取服务会接收此广播
		Log.i(TAG, "MsgActivity发送界面开启广播");
		Intent intent = new Intent(Constant.ACTION_MSG_SHOW);
		Bundle value = new Bundle();
		value.putBoolean("flag", true);
		value.putInt(MsgListTable.USER_ID, mUserID);
		intent.putExtra("bundle", value);
		sendBroadcast(intent);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case android.R.id.home:
				finish();
				return true;

			default:
				return super.onOptionsItemSelected(item);
		}
	}

	/*@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		Bundle data = intent.getBundleExtra("bundle_msg");
		if (data != null)
		{
			Log.i(TAG, "## list_bundle!=null ##");
			mUserID = data.getInt(MsgFragment.ARG_USERID);
			mFragment = MsgFragment.createInstance(data);
		}

		getActionBar().setDisplayHomeAsUpEnabled(true);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		//ft.add(R.id.container, mFragment, FTAG_MSG).commit();
		ft.replace(R.id.container, mFragment, FTAG_MSG).commit();
	}*/

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		unregisterReceiver(mReceiver);

		// 发送界面关闭广播
		Log.i(TAG, "MsgActivity发送界面关闭 广播");
		Intent intent = new Intent(Constant.ACTION_MSG_SHOW);
		Bundle bundle = new Bundle();
		bundle.putBoolean("flag", false);
		bundle.putInt(MsgListTable.USER_ID, mUserID);
		intent.putExtra("bundle", bundle);
		sendBroadcast(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
		{
			finish();
		}
		return true;
	}

	public class MsgReceiver extends BroadcastReceiver
	{

		@Override
		public void onReceive(Context context, Intent intent)
		{
			// TODO Auto-generated method stub

			if (intent.getAction().equals(Constant.ACTION_NEW_MSGS))
			{
				Log.i(TAG, "接收到刷新新消息的广播");
				((MsgFragment) mFragment).updateMsgs();
			}
		}

	}
}
